<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Default controller.
 */
class Order_Controller extends Template_Controller 
{
	public function __construct()
	{
		parent::__construct();
		
		// Add the menu to the template
		$this->template->menu = new View('menu');
		$this->template->content = '';
	}
	
	public function index()
	{
		self::lists(1, 1);
	}

	public function lists($order_status, $page_no)
	{
		$page_size = 10;
	    $model = new Order_Model();
		
		$this->pagination = new Pagination(array(
	        'base_url'    => 'order/lists/'.$order_status.'/', 
	        'uri_segment' => 4, 
			'items_per_page' => $page_size, 
	        'total_items' => $model->select('COUNT(*) AS records_found')->where('status', $order_status)->find()->records_found
	    ));
	 
		$view = new View('order/lists');
		$view->datas = $model->where('status', $order_status)
							 ->orderby('id', 'desc')
							 ->limit($page_size, (($page_no - 1) * $page_size))
							 ->find_all();
		
		$status = Kohana::lang('common.a_order_status');
		
		/*foreach($status as $key=>$value)
		{
			if($key < $order_status)
			{
				unset($status[$key]);
			}
		}*/
		
		$view->order_status = $status;
		$view->old_order_status = $order_status;
		
		$this->template->content = $view->render();
	}
	
	public function change_status($order_id, $old_status, $order_status)
	{
		$model = new Order_Model($order_id);
		$model->status = $order_status;
		$model->save();
		
		url::redirect('order/lists/'.$old_status.'/1', 301);
	}
}